package com.longge.wechat.controller.wx;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.longge.wechat.communication.Poster;

import com.longge.wechat.message.resp.Article;
import com.longge.wechat.message.resp.RespArticleMessage;
import com.longge.wechat.message.resp.RespTextMessage;
import com.longge.wechat.utils.ReadProperties;
import com.longge.wechat.utils.SignUtil;
import com.longge.wechat.utils.SinaWeatherAsker;
import com.longge.wechat.utils.XmlUtil;

@Controller
public class WelcomeController {
	
	private final static String WELCOME = "感谢关注XX旅游微信公众平台!\n";
	@RequestMapping(value ="/welcome.do",method = RequestMethod.POST)
	public void welcome(HttpServletRequest request, HttpServletResponse response){
		//从请求中取出openID
		String openId = (String) request.getAttribute("openId");
		String from = (String) request.getAttribute("from");
		
		
		String eventKey = (String) request.getAttribute("EventKey");
		
		if(SignUtil.isNotEmpty(eventKey)){
			//扫描带参数的二维码关注
			if(eventKey.contains("qrscene_")){
				eventKey = eventKey.substring(8);
			}
			int imageId = Integer.valueOf(eventKey);
			String rs = "对不起，不能重复扫描二维码点赞！";
			
			//发送图文链接消息
			//读取配置文件中的域名
			String domainName = ReadProperties.loadProperties().getProperty("domainName");
			
			Article article1 = new Article("点击查看已点赞图片详情页面", rs, "", "");

			List<Article> articles = new ArrayList<Article>();
			articles.add(article1);

			RespArticleMessage postedNewsMessage = new RespArticleMessage(
					(String) request.getAttribute("openId"),
					(String) request.getAttribute("from"));
			postedNewsMessage.setArticles(articles);

			XmlUtil xmlUtil = XmlUtil.getInstance();
			String xml = xmlUtil.bean2xml(postedNewsMessage,null,null).replaceAll(
					"com.longge.wechat.message.resp.Article", "item");

			Poster.post(response, xml);
		}else{
			RespTextMessage message = new RespTextMessage(openId,from,WELCOME);
			Poster.post(response, XmlUtil.getInstance().bean2xml(message,null,null));
		}
		
	}
	
	@RequestMapping(value = "/unsubscribe.do", method = RequestMethod.POST)
	public void unsubscribe(HttpServletRequest request, HttpServletResponse response) {
		
		//从请求中取出openID
		String openId = (String) request.getAttribute("openId");

	}
	
	@RequestMapping(value = "/getWeather.do", method = RequestMethod.POST)
	public void getWeather(HttpServletRequest request, HttpServletResponse response) {
		
		//从请求中取出openID
		String openId = (String) request.getAttribute("openId");
		
		
		
		//读取配置文件中的域名
		String domainName = ReadProperties.loadProperties().getProperty("domainName");
		String picUrl = domainName + "resources/images/weather.jpg";
		String url = "http://weather1.sina.cn/?city=%E6%9D%AD%E5%B7%9E&vt=4";
		
		SinaWeatherAsker swa = new SinaWeatherAsker();
		String weatherMsg = swa.getWeather();
		
		Article article1 = new Article("点击查看天气预报详情", weatherMsg, picUrl, url);

		List<Article> articles = new ArrayList<Article>();
		articles.add(article1);
		
		RespArticleMessage postedNewsMessage = new RespArticleMessage(
				(String) request.getAttribute("openId"),
				(String) request.getAttribute("from"));
		postedNewsMessage.setArticles(articles);

		XmlUtil xmlUtil = XmlUtil.getInstance();
		String xml = xmlUtil.bean2xml(postedNewsMessage,null,null).replaceAll(
				"com.longge.wechat.message.resp.Article", "item");

		Poster.post(response, xml);
	}
	
	@RequestMapping(value = "/getParking.do", method = RequestMethod.POST)
	public void getParking(HttpServletRequest request, HttpServletResponse response) {
		
		//从请求中取出openID
		String openId = (String) request.getAttribute("openId");
		String from = (String) request.getAttribute("from");
		
		String parkingMsg = "对不起，此功能尚未实现！";
		RespTextMessage message = new RespTextMessage(openId,from,parkingMsg);
		Poster.post(response, XmlUtil.getInstance().bean2xml(message,null,null));
	}
	
	public static void main(String[] args) {
		System.out.println("".contains(""));
	}
	
}
